package com.quyeying.controller;

import com.quyeying.utils.JwtUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import org.springframework.web.bind.annotation.*;
import com.quyeying.service.TblPopularizeVideoService;
import com.quyeying.entity.TblPopularizeVideoEntity;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* tbl_popularize_video 前端控制器
* </p>
* @author skx
* @since 2022-10-20
*/
@Slf4j
@RestController
@RequestMapping("popularizeVideoEntity")
public class TblPopularizeVideoController {

    @Resource
    private TblPopularizeVideoService tblPopularizeVideoService;

    /**
    * 新增
    **/
    @PostMapping("/add")
    public R add(HttpServletRequest request, @RequestBody TblPopularizeVideoEntity tblPopularizeVideoEntity){
        try {
            String account = JwtUtil.getUserName(request.getHeader("Authorization"));
            return tblPopularizeVideoService.add(tblPopularizeVideoEntity, account);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 删除
    **/
    @DeleteMapping("/del")
    public R delete(Long id){
        try {
            return tblPopularizeVideoService.delete(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * 更新
    **/
    @PostMapping("/update")
    public R update(@RequestBody TblPopularizeVideoEntity tblPopularizeVideoEntity){
        try {
            return tblPopularizeVideoService.updateData(tblPopularizeVideoEntity);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 商户端-视频列表分页查询
     **/
    @GetMapping("/findListByPage")
    public R findListByPage(HttpServletRequest request, @RequestParam Integer current,
                            @RequestParam Integer size){
        try {
            String account = JwtUtil.getUserName(request.getHeader("Authorization"));
            //app商家端按sort排序
            String end = "merchant";
            return tblPopularizeVideoService.merchantVideoPage(request, current, size, account,end);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblPopularizeVideoService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }
}
